package edu.uoc.tdp.pac4.common;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class ConnectionRMI {

	private static RemotInterface rmiServidor;

	private static Registry registro;

	private static String direccionServidor = "127.0.0.1";

	private static String puertoServidor = "3232";
	
	public static RemotInterface conectarseAlServidor() {
		if(rmiServidor != null){
			return rmiServidor;
		}else{
			try {
				// obtener el registro
				registro = LocateRegistry.getRegistry(direccionServidor,
						(new Integer(puertoServidor)).intValue());
				// creando el objeto remoto
				rmiServidor = (RemotInterface) (registro.lookup("rmi://localhost/eOlympic"));
				
				return rmiServidor;
				
			} catch (RemoteException e) {
				GestorExceptions.sentException(GestorExceptions.ERR_REG_RMI);
			} catch (NotBoundException e) {
				GestorExceptions.sentException(GestorExceptions.ERR_REG_RMI);
			}
		}
		return null;
	}
}
